package com.ruoyi.project.wx.service.impl;


import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.wx.dao.SetExpressDao;
import com.ruoyi.project.wx.dao.ShopExpressDetailsDao;
import com.ruoyi.project.wx.dao.ShopExpressMessageDao;
import com.ruoyi.project.wx.domain.SetExpressDO;
import com.ruoyi.project.wx.domain.ShopExpressDetailsDO;
import com.ruoyi.project.wx.domain.ShopExpressMessageDO;
import com.ruoyi.project.wx.service.ShopExpressMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ShopExpressMessageServiceImpl implements ShopExpressMessageService {
	@Autowired
	private ShopExpressMessageDao shopExpressMessageDao;
	@Autowired
	private ShopExpressDetailsDao shopExpressDetailsDao;
	@Autowired
	private SetExpressDao setExpressDao;
	
	@Override
	public ShopExpressMessageDO get(String id){
		return shopExpressMessageDao.get(id);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public ShopExpressMessageDO queryByOrderId(String orderId) {
		ShopExpressMessageDO messageDO = shopExpressMessageDao.queryByOrderId(orderId);
		if (messageDO!=null){
			//如果快递已经被签收则不再进行查询请求，直接从数据库查找
			if(messageDO.getStatus()==3||messageDO.getStatus()==4){
				messageDO.setExpressDetailsList(shopExpressDetailsDao.queryByOrderId(orderId));
			}else{
				//进行物流信息的查询
				SetExpressDO setExpressDO = setExpressDao.list(new HashMap<String, Object>()).get(0);
				//ExpressUtil.getExpressMessage(messageDO,setExpressDO);
				//如果查询后发现当前的快递状态变为收货或者问题件，进行快递详情的数据库插入操作
				if(messageDO.getStatus()==3||messageDO.getStatus()==4){
//					super.updateIgnoreNull(message);
					update(messageDO);
					List<ShopExpressDetailsDO> detailsList = messageDO.getExpressDetailsList();
					if (detailsList!=null&&detailsList.size()>0){
						for (ShopExpressDetailsDO detailsDO:detailsList){
							detailsDO.setId(UuidUtil.get32UUID());
							shopExpressDetailsDao.save(detailsDO);
						}
					}
				}
			}
			//换顺序
			List<ShopExpressDetailsDO> detailsDOList = messageDO.getExpressDetailsList();
			Collections.reverse(detailsDOList); // 倒序排列
		}

		return messageDO;
	}

	@Override
	public List<ShopExpressMessageDO> list(Map<String, Object> map){
		return shopExpressMessageDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return shopExpressMessageDao.count(map);
	}
	
	@Override
	public int save(ShopExpressMessageDO shopExpressMessage){
		return shopExpressMessageDao.save(shopExpressMessage);
	}
	
	@Override
	public int update(ShopExpressMessageDO shopExpressMessage){
		return shopExpressMessageDao.update(shopExpressMessage);
	}
	
	@Override
	public int remove(String id){
		return shopExpressMessageDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return shopExpressMessageDao.batchRemove(ids);
	}
	
}
